package com.item.mall.controller;


import com.item.mall.entity.User;
import com.item.mall.entity.UserAddress;
import com.item.mall.service.CartService;
import com.item.mall.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张津瑞、彭家博
 * @since 2021-07-31
 */
@Controller
@RequestMapping("/userAddress")
public class UserAddressController {
    @Autowired
    CartService cartService;
    @Autowired
    UserAddressService userAddressService;

    @GetMapping("/getAddressList")
    public ModelAndView getAddressList(HttpSession session){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("userAddressList");
        modelAndView.addObject("addressList",userAddressService.list());
        User user =(User) session.getAttribute("user");
        modelAndView.addObject("cartList",cartService.getCartList(user.getId()));
        return modelAndView;
    }

    @GetMapping("/addAddress/{consignee}/{phone}/{province}/{city}/{area}/{address}/{type}")
    public ModelAndView addAddress(@PathVariable("consignee") String consignee,
                                   @PathVariable("phone") String phone,
                                   @PathVariable("province") String province,
                                   @PathVariable("city") String city,
                                   @PathVariable("area") String area,
                                   @PathVariable("address") String address,
                                   @PathVariable("type") String type,
                                   HttpSession session){
        ModelAndView modelAndView =new ModelAndView(type);
        userAddressService.save(new UserAddress(((User)session.getAttribute("user")).getId(),consignee,province,city,area,address,phone,0));
        User user =(User) session.getAttribute("user");
        modelAndView.addObject("cartList",cartService.getCartList(user.getId()));
        modelAndView.addObject("addressList",userAddressService.list());
        return modelAndView;
    }

    @GetMapping("/deleteAddress/{id}/{type}")
    public ModelAndView deleteAddress(@PathVariable("id") Integer id,
                                      @PathVariable("type") String type,
                                      HttpSession session){
        ModelAndView modelAndView =new ModelAndView(type);
        userAddressService.removeById(id);
        User user =(User) session.getAttribute("user");
        modelAndView.addObject("cartList",cartService.getCartList(user.getId()));
        modelAndView.addObject("addressList",userAddressService.list());
        return modelAndView;
    }

    @GetMapping("/setDefault/{id}/{type}")
    public ModelAndView setDefault(@PathVariable("id") Integer id,
                                      @PathVariable("type") String type,
                                      HttpSession session){
        ModelAndView modelAndView =new ModelAndView(type);
        User user =(User) session.getAttribute("user");
        userAddressService.setDefault(user.getId(),id);
        modelAndView.addObject("cartList",cartService.getCartList(user.getId()));
        modelAndView.addObject("addressList",userAddressService.list());
        return modelAndView;
    }


    @GetMapping("/updateAddress/{id}/{consignee}/{phone}/{province}/{city}/{area}/{address}/{type}")
    public ModelAndView updateAddress(UserAddress userAddress,
                                      @PathVariable("type") String type,
                                      HttpSession session){
        ModelAndView modelAndView=new ModelAndView(type);
        User user =(User) session.getAttribute("user");
        userAddressService.updateById(userAddress);
        modelAndView.addObject("addressList",userAddressService.list());
        modelAndView.addObject("cartList",cartService.getCartList(user.getId()));
        return modelAndView;
    }

    @GetMapping("/getAddress/{addressId}/{type}")
    public ModelAndView getAddress(@PathVariable("addressId") Integer addressId,
                                   @PathVariable("type") String type,
                                   HttpSession session){
        ModelAndView modelAndView=new ModelAndView("updateAddress");
        User user =(User) session.getAttribute("user");
        modelAndView.addObject("type",type);
        modelAndView.addObject("cartList",cartService.getCartList(user.getId()));
        modelAndView.addObject("address",userAddressService.getById(addressId));
        return modelAndView;
    }

}

